
import UIKit



//MARK: - 定义协议
protocol VistorViewDelegte: NSObjectProtocol {
   
    func vistorViewRegistClick()
    func vistorViewLoginClick()
    
}

class VistorView: UIView {
    
    //代理属性
    var vistorViewDelegte: VistorViewDelegte?
    
    //MARK: - 按钮点击事件
    ///注册按钮点击响应事件
    func registClick(){
        vistorViewDelegte?.vistorViewRegistClick()
    }
    ///登录按钮点击响应事件
    func loginClick(){
        vistorViewDelegte?.vistorViewLoginClick()
    }
    
    
    

    
    // MARK: - 构造函数
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        prepareUI()
    }
    
    /**
     准备UI
    */
    func prepareUI() {
        
        
        backgroundColor = UIColor(white: 237/255 , alpha: 1)
        
         addSubview(iconView) //转轮
        /**
        添加遮罩视图
        */
        addSubview(coverView)
        //添加子控件/
        addSubview(homeView)   //小房子
        addSubview(messageLable) //消息
        addSubview(registerButton)//注册
        addSubview(loginButton)//登录

      
        
        
        //添加约束
        
        iconView.translatesAutoresizingMaskIntoConstraints = false//转轮
        coverView.translatesAutoresizingMaskIntoConstraints = false//遮罩
        homeView.translatesAutoresizingMaskIntoConstraints = false//主页
        messageLable.translatesAutoresizingMaskIntoConstraints = false//信息
        registerButton.translatesAutoresizingMaskIntoConstraints = false//注册
        loginButton.translatesAutoresizingMaskIntoConstraints = false//登录
     
        
        
        
        

        let cX = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.addConstraint(cX)


        let cY = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
        self.addConstraint(cY)
        


        //设置小房子
      addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))

        //消息label
        //X
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))

        //Y
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))

        //Width
        addConstraint(NSLayoutConstraint(item: messageLable, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
//

        //注册按钮
        //左边
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //顶部
       addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width , relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
         addConstraint( NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height , relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
//
        //登录按钮
        //右边
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
       
        
        //顶部
          addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLable, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
     
        //宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant:100))
      
        //高度
          addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //遮罩
        //左边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        //顶部
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //右边
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //下面
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        



    }
   
    func test(){
        let cX = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
        self.addConstraint(cX)
        
        let cY = NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -200)
        self.addConstraint(cY)
    }
    
    //MARK: - 转轮动画
    func startrptatopnAnimation() {
        //创建动画
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        //设置参数
        animation.toValue = 2 * M_PI
        animation.repeatCount = MAXFLOAT
        //设置动画时间
        animation.duration = 20
        
        //完成的时候不移除
        animation.removedOnCompletion = false
        
        //开始动画
        iconView.layer.addAnimation(animation, forKey: "homeRotation")
        
        
        
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    
    //MARK:  - 懒加载
    //图片转动
    private lazy var iconView: UIImageView = {
        let imageView = UIImageView()
        let image = UIImage(named: "visitordiscover_feed_image_smallicon")
        imageView.image = image
        
        return imageView
    }()
    
    
    //添加房子
    private lazy var homeView: UIImageView = {
       let imageView = UIImageView()
        
        let image = UIImage(named: "visitordiscover_feed_image_house")
        imageView.sizeToFit()
        imageView.image = image
        
        return imageView
 
    }()
    
    
    
    ///消息
    private lazy var messageLable: UILabel = {
       
        let  label = UILabel()
        label.text = "Be slow to promise and quick to perform                                                             ---- 不轻诺，诺必果。"
        label.textColor = UIColor.orangeColor()//颜色
        label.numberOfLines = 0//显示多行
        label.sizeToFit()//自动拉伸
        
        return label
    }()

    ///=======添加 < 注册  > 按钮
    private lazy var registerButton: UIButton = {
    let button                                = UIButton()

        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:UIControlState.Normal)
//        button.sizeToFit()
       
        ///添加注册按钮的点击事假
        button.addTarget(self, action: "registClick", forControlEvents: UIControlEvents.TouchUpInside)

        return button
    }()

    //======添加  <  登录   > 按钮
    private lazy var loginButton: UIButton = {
    let button                             = UIButton()

        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:UIControlState.Normal)
        button.sizeToFit()
        
        ///登录按钮的点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)


        return button
    }()
    
    
    //遮罩
    private lazy var coverView: UIImageView = {
        
        let image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return UIImageView(image: image)
    }()
    
    
    
    //MARK: - 创建访客视图内容
    /**
    设置访客视图内容/除了首页
    - parameter imageName: 图片名称
    - parameter message:   介绍内容
    */
    func setupInfo(imageName:String,message:String)
    {
        //隐藏房子
        homeView.hidden = true
        
        iconView.image = UIImage(named: imageName)
        
        messageLable.text = message
//        messageLable.sizeToFit()
//        
//       
//        sendSubviewToBack(coverView)
        self.sendSubviewToBack(coverView)
    }
    
   
    
    
    
}
